package com.example.NetworkSender;

import com.example.NetworkSender.network.NetworkManager;


public class Engine {
    NetworkManager manager;
    int[] bufferRead = new int[10];

    public Engine(int port) {
        initManager(port);
    }

    public void send(int[] data, String address) {
        manager.sendData(address, data, data.length);
    }

    public String receive() throws IllegalStateException {
        if (manager == null) {
            throw new IllegalStateException("Engine.receive: manager is null");
        }
        String result;
        int size = manager.getData(bufferRead);
        if (size > 0) {
            StringBuilder sb = new StringBuilder(size * 2);
            for (int i = 0; i < size; i++) {
                sb.append(bufferRead[i]).append(' ');
            }
            result = sb.toString();
        } else {
            result = "ничего не прислали";
        }
        return result;
    }

    public void initManager(int port) {
        manager = new NetworkManager(port);
    }

    public boolean readyToSend() {
        return manager != null;
    }
}
